// pages/details/details.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    aaa:false,
    class:"",
    article: {},
    review_text: '',
    review_input_id: 0,
    parent_id: 0,
    model_show: 1,
    is_detail_show: 0,
    statusbar:0,
    is_auto_focus: false,
    reply_user_name: ''
  },
  zk:function(){
    this.setData({
      aaa:false,
      class:""
    })
  },
  sq:function(){
    this.setData({
      aaa:true,
      class:"aa"
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (t) {
    getApp().page.onLoad(this, t),this.setData({
      is_auto_focus: t.is_review == 1 ? true : false
    }), this.loadData(t);
    wx.setNavigationBarTitle({
      title: '帖子详情'
    })
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    })
  },
  loadData: function (t) {
    var a = this;
    getApp().core.showLoading({
      title: "加载中"
    }), getApp().request({
      url: getApp().api.weitao.article_detail,
      data: {
        article_id: t.article_id
      },
      success: function (t) {
        0 == t.code && a.setData({
          article: t.data.detail
        }), 1 == t.code && (getApp().core.showModal({
          title: "提示",
          content: t.msg,
          showCancel: !1,
          success: function (t) {
            if (t.confirm) {
              wx.navigateBack({
                delta: 1, // 返回上一级页面。
                success: function () {
                  console.log('成功！')
                }
              })
            }
            console.log(t.confirm);
          }
        }));
        
    // a.data.article.content && wx.setNavigationBarTitle({
    //   title: a.data.article.content.substring(0,5)
    // })

      },
      complete: function () {
        getApp().core.hideLoading();
      }
    });
  },
  share_return() {
    return false;
  },
  /**
   * @param {*} e
   * 点赞以及取消点赞 
   */
  like_action(e) {
    var s = this;
    var like_data = 0;
    var review = e.currentTarget.dataset.review ? e.currentTarget.dataset.review : 0;
    if (review) {
      review.is_liked == 1 ? like_data = 0 : like_data = 1;
      let like_count = review.like_count;
    } else {
      this.data.article.is_liked == 1 ? like_data = 0 : like_data = 1;
      let like_count = this.data.article.like_count;
    }
    if (this.data.article.id > 0) {
      var a = this;
      getApp().core.showLoading({
        title: "加载中"
      }), getApp().request({
        url: getApp().api.weitao.like_article,
        data: {
          article_id: a.data.article.id,
          is_liked: like_data,
          review_id: review.id ? review.id : 0
        },
        success: function (t) {
          if (t.code == 0) {
            a.loadData({
              article_id: a.data.article.id
            });
          }
          getApp().core.showToast({
            title: t.msg,
            icon: 'none'
          });
        },
        complete: function () {
          getApp().core.hideLoading();
        }
      });
    } else {
      getApp().core.showToast({
        title: "系统错误",
        icon: 'none'
      });
    }
  },
  //跳转页面
  goUrl: function (e) {
    var gopage = e.currentTarget.dataset.url;
    if (!gopage) {
      return;
    }
    getApp().core.navigateTo({
      url: gopage
    });
  },
  /**
   * 
   * @param {*} e
   * 评论内容 
   */
  review_input(e) {
    this.setData({
      review_text: e.detail.value
    });
  },
  review_submit(e) {
    var s = this,
      article_id = s.data.article.id,
      parent_id = s.data.parent_id,
      review_text = s.data.review_text;
    if (article_id > 0) {
      var a = this;
      getApp().core.showLoading({
        title: "加载中"
      }), getApp().request({
        url: getApp().api.weitao.review_article,
        data: {
          article_id: article_id,
          parent_id: parent_id,
          review_text: review_text
        },
        success: function (t) {
          if (t.code == 0) {
            s.data.article.discuss_count = parseInt(s.data.article.discuss_count) + 1;
            a.setData({
              article: s.data.article,
              review_text: ''
            });
            s.loadData({
              article_id: article_id
            });
          }
          getApp().core.hideLoading();
          getApp().core.showToast({
            title: t.msg,
            icon: 'none'
          });
          
        },
        fail: function () {
          getApp().core.hideLoading();
        },
        complete: function () {
          
        }
      });
    } else {
      getApp().core.showToast({
        title: "系统错误",
        icon: 'none'
      });
    }

  },
  model_action(e) {
    this.setData({
      model_show: 1,
      parent_id: e.currentTarget.dataset.review ? e.currentTarget.dataset.review.id : 0,
      reply_user_name: e.currentTarget.dataset.review ? e.currentTarget.dataset.review.nickname : '',
    });
    if(e.currentTarget.dataset.action == 1){
      this.setData({
        model_show: 0,
        is_auto_focus: true
      });
      this.setData({
        model_show: 1,
      });
    }
    return false;
  },
  show_detail(e) {
    this.setData({
      is_detail_show: this.data.is_detail_show == 1 ? 0 : 1
    });
  },
  show_all(e){
    var index = e.currentTarget.dataset.reviewIndex,s = this;
    s.data.article.review[index].review_show_all = 1;
    s.setData({
      article: s.data.article
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  onShareAppMessage: function (res) {
    getApp().page.onShareAppMessage(this);
    if (res.from === 'button') {
      var article = res.target.dataset.article;
      console.log(article);
      return {
        title: article.content,
        path: '/weitao/details/details?article_id=' + article.id + '&store_id=' + (wx.getStorageSync("store_id") ? wx.getStorageSync("store_id") : 0),
        success: function (res) {
          console.log('成功', res)
        }
      }
    }
    return {
      title: '微淘首页',
      path: '/weitao/index/index',
      success: function (res) {
        console.log('成功', res)
      }
    }
  },
})